package com.dianrui.qiyouriding;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.util.Log;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.SecretProtocolAdapter;
import com.dianrui.qiyouriding.update.OKHttpUpdateHttpService;
import com.dianrui.qiyouriding.util.GlideImageLoader;
import com.dianrui.qiyouriding.util.Static;
import com.tbit.tbitblesdk.Bike.TbitBle;
import com.tencent.bugly.crashreport.CrashReport;
import com.xuexiang.xupdate.XUpdate;
import java.util.Set;
import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ImageLoader;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MyApplication extends Application {

    private static FunctionConfig functionConfig;
    private ImageLoader imageloader;
    private CoreConfig coreConfig;


    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(getApplicationContext());
        SDKInitializer.setCoordType(CoordType.BD09LL);
        initThiredServices();
        Static.CONTEXT = this;
    }

    public void initThiredServices(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                initJiGuang();
//                CrashReport.initCrashReport(getApplicationContext(), "724fa2d348", true);
//                CrashReport.setIsDevelopmentDevice(getApplicationContext(), BuildConfig.DEBUG);
                TbitBle.initialize(getApplicationContext(), new SecretProtocolAdapter());
                initCameraConfig();
                repairAndroidOsFileUriExposedException();
                loadXupdate();
            }
        }.start();
    }

    private void loadXupdate() {
        XUpdate.get().isWifiOnly(false).isGet(true).isAutoMode(false).supportSilentInstall(true).setIUpdateHttpService(new OKHttpUpdateHttpService()).init(this);
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }




    public void repairAndroidOsFileUriExposedException() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }
    }

    public void initCameraConfig() {
        if (functionConfig == null) {
            //配置功能
            functionConfig = new FunctionConfig.Builder().setMutiSelectMaxSize(9)
                    .setEnableEdit(true)
                    .setEnableCrop(true)
                    .setEnableRotate(false)
                    .setEnableCamera(false)
                    .setCropSquare(true)
                    .setRotateReplaceSource(false)
                    .setCropReplaceSource(false)
                    .setForceCrop(false)
                    .setForceCropEdit(true)
                    .setEnablePreview(true)
                    .build();
            //配置imageloader
            imageloader = new GlideImageLoader();
            //核心配置
            coreConfig = new CoreConfig.Builder(getApplicationContext(), imageloader, new ThemeConfig.Builder().setTitleBarBgColor(getResources().getColor(R.color.main_color)).build()).setFunctionConfig(functionConfig).build();
            GalleryFinal.init(coreConfig);
        }

    }

    public static FunctionConfig getFunctionConfig() {
        return functionConfig;
    }

    public void initJiGuang() {
        JPushInterface.init(this);
        JPushInterface.setDebugMode(false);
    }

    /**
     * 设置别名
     *
     * account
     */
    public static void initJiGuangUser(final String account, Context context) {
        JPushInterface.setAlias(context, account, new TagAliasCallback() {
            @Override
            public void gotResult(int code, String s, Set<String> set) {
                switch (code) {
                    case 0:
                        Log.e(account + "别名注册成功", s);
                        // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                        break;
                    case 6002:
                        Log.e(account +"别名注册失败了", s);
                        break;
                        default:
                            break;

                }
            }
        });
    }

}
